from rest_framework import serializers
from .models import User, CommissionCategory, Attendance

class UserSerializer(serializers.ModelSerializer):
    role_display = serializers.CharField(source='get_role_display', read_only=True)
    
    class Meta:
        model = User
        fields = '__all__'
        extra_kwargs = {
            'pwd': {'write_only': True}
        }

class CommissionCategorySerializer(serializers.ModelSerializer):
    # 添加关联产品列表
    products = serializers.SerializerMethodField()
    
    class Meta:
        model = CommissionCategory
        fields = '__all__'
    
    def get_products(self, obj):
        """获取与提成分类关联的产品列表"""
        from apps.OMM.models import Product
        products = Product.objects.filter(commission_category=obj).values('id', 'product_name')
        return products

class AttendanceSerializer(serializers.ModelSerializer):
    nickname_display = serializers.CharField(source='nickname.nickname', read_only=True)
    
    class Meta:
        model = Attendance
        fields = '__all__' 